/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Properties;

/**
 *
 * @author neo
 */
public abstract class ObjectHandler {

    private Properties prop = null;

    public abstract Object parseObject(ParserObject parser, Hashtable table, Properties prop);

    protected void setProperties(Properties prop) {
        this.prop = prop;
    }

    protected void setString(Object obj, String name, String param) throws Exception {
        Method method = obj.getClass().getMethod(name, new Class[]{String.class});
        method.invoke(obj, new Object[]{param.substring(1, param.length() - 1)});
    }

    protected void setInt(Object obj, String name, String sparam) throws Exception {
        String relValue = sparam.replaceAll("\"", "");
        int param = Integer.parseInt(relValue);
        Method method = obj.getClass().getMethod(name, new Class[]{int.class});
        method.invoke(obj, new Object[]{param});
    }
    
    protected void setInteger(Object obj, String name, String sparam) throws Exception {
        String relValue = sparam.replaceAll("\"", "");
        Integer param = new Integer(relValue);
        Method method = obj.getClass().getMethod(name, new Class[]{Integer.class});
        method.invoke(obj, new Object[]{param});
    }

    protected void setDate(Object obj, String name, String sparam) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String realParam = sparam;
        System.out.println("Real param:"+ sparam);
        if (sparam.startsWith("\"")) {
            realParam = sparam.substring(1, (sparam.length() - 1));
        }
        if( realParam.length() > 0 ) {
                   java.util.Date param = df.parse(realParam);
        Method method = obj.getClass().getMethod(name, new Class[]{java.util.Date.class});
        method.invoke(obj, new Object[]{param});
        }
//        java.util.Date param = df.parse(realParam);
//        Method method = obj.getClass().getMethod(name, new Class[]{java.util.Date.class});
//        method.invoke(obj, new Object[]{param});
    }

    protected void setLong(Object obj, String name, String sparam) throws Exception {
        long param = Long.parseLong(sparam);
        Method method = obj.getClass().getMethod(name, new Class[]{long.class});
        method.invoke(obj, new Object[]{param});
    }

    protected void setFloat(Object obj, String name, String sparam) throws Exception {
        float param = 0;
        try {
        param = Float.parseFloat(sparam);
        } catch(Exception ex) { }
        Method method = obj.getClass().getMethod(name, new Class[]{float.class});
        method.invoke(obj, new Object[]{param});
    }

    protected void setDouble(Object obj, String name, String sparam) throws Exception {
        double param = Double.parseDouble(sparam);
        Method method = obj.getClass().getMethod(name, new Class[]{double.class});
        method.invoke(obj, new Object[]{param});
    }

    protected void setBoolean(Object obj, String name, String sparam) throws Exception {
        boolean param = Boolean.parseBoolean(sparam);
        Method method = obj.getClass().getMethod(name, new Class[]{boolean.class});
        method.invoke(obj, new Object[]{param});
    }

//    protected int isType(String s) {
//        if (isDate(s)) {
//            return 3;
//        }
//        if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
//            return 4;
//        }
//        if (s.startsWith("\"") || s.endsWith("\"")) {
//            return 0;
//        }
//        if (isNumeric(s)) {
//            if (s.indexOf(".") > -1) {
//                return 2;
//            } else {
//                return 1;
//            }
//        }
//        return 0;
//    }
//
    protected int isType(Class cls) {
        //System.out.println("isType:"+ cls.getName());
        try {
        String scase = prop.getProperty(cls.getName()+".set");
        return Integer.parseInt(scase);
        } catch(Exception ex) {
             //System.out.println("isTypeEx:"+ cls.getName());
            ex.printStackTrace();
        }
        return -1;
    }

//    private boolean isDate(String s) {
//        if (s.indexOf(":") > -1) {
//            return true;
//        }
//        return false;
//    }
//
//    private boolean isNumeric(String s) {
//        char c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
//        char cs = s.charAt(0);
//        for (int i = 0; i < c.length; i++) {
//            if (c[i] == cs) {
//                return true;
//            }
//        }
//        return false;
//    }

    protected String toSetMethodName(String tableName) {
        StringBuffer m = new StringBuffer(tableName);
        StringBuffer ch = new StringBuffer("set");
        ch.append(tableName.substring(0, 1).toUpperCase());
        boolean cf = false;
        for (int i = 1; i <
                m.length(); i++) {
            char c = m.charAt(i);
            if (cf == true) {
                ch.append(Character.toUpperCase(c));
                cf =
                        false;
            } else {
                if (c == '_') {
                    cf = true;
                } else {
                    ch.append(c);
                }

            }
        }
        return ch.toString();
    }

    protected String toGetMethodName(String tableName) {
        StringBuffer m = new StringBuffer(tableName);
        StringBuffer ch = new StringBuffer("get");
        ch.append(tableName.substring(0, 1).toUpperCase());
        boolean cf = false;
        for (int i = 1; i <
                m.length(); i++) {
            char c = m.charAt(i);
            if (cf == true) {
                ch.append(Character.toUpperCase(c));
                cf =
                        false;
            } else {
                if (c == '_') {
                    cf = true;
                } else {
                    ch.append(c);
                }

            }
        }
        return ch.toString();
    }
}
